package netWork;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 客户端
 */
public class Home04ClientB {
    public static void main(String[] args) throws Exception {
        try (Socket socket = new Socket("127.0.0.1",9999);
            Scanner scanner = new Scanner(System.in);
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))){
            System.out.println("请输入要查找的歌名");
            String name = scanner.next();
            //将名字发给服务器
            writer.println(name);
            writer.flush();
            socket.shutdownOutput();
            name = reader.readLine();//返回名字
            System.out.println(name);
            byte[]array = UtilAPi.getByteArray(bis);//返回文件
            try (OutputStream ops = new FileOutputStream("src\\music/"+name)){
                ops.write(array);//将该文件写入新地址
                ops.flush();
                System.out.println("下载成功");
            }

        }
    }
}
